package org.testis.gui.mvc.controller.testdesign;

import org.testis.gui.mvc.controller.*;
import org.testis.gui.mvc.model.testdesign.ProfileSettingDetailsModel;
import org.testis.gui.mvc.utilities.AtofProfileKeyTableModel;
import org.testis.gui.mvc.view.testdesign.ConditionsTextEditorView;
import org.testis.gui.mvc.view.testdesign.ProfileSettingDetailsView;
import java.util.List;

import org.testis.orm.classes.T_test_profiles;
import org.testis.orm.refinedclasses.TestDesignTreeNode;
import org.testis.orm.refinedclasses.TestDesignTreeRefined.TestDesignTreeParameters;
import org.testis.utils.middleware.GlobalDataHandler;

/**
 *
 * @author nurhat
 */
public class ProfileSettingDetailsController extends AbstractController {

    public static final String SHOW_VIEW = "ViewVisibility";
    public static final String PARAMETER_MODEL_CHANGED = "Model";
    public static final String SHOW_ERROR = "LastError";
    public static final String CURRENT_PROFILE_ID = "CurrentProfileId";
    public static final String CURRENT_SELECTED_KEYNAME = "KeyName";
    public static final String CURRENT_SELECTED_KEYVALUE = "KeyValue";
    private boolean m_isModal = true;
    private boolean m_useForCondition = false;
    private boolean m_useForDataObject = false;

    public ProfileSettingDetailsController() {
        super();
        TestDesignControllerList.newInstance().
                setProfileSettingDetailsController(this);

    }

    public void startLoader(int currentProfileId) {
        if (!isValidProfile(currentProfileId)) {
            return;
        }
        m_isModal = true;
        initialize(true);
        setModelProperty(CURRENT_PROFILE_ID,
                currentProfileId);
        setModelProperty(SHOW_VIEW,
                true);

    }

    public boolean isValidProfile(int currentProfileId) {
        Integer profile = currentProfileId;
        if (profile != null) {
            GlobalDataHandler handler = new GlobalDataHandler();
            if (handler.getProfileById(profile) != null) {
                return true;
            }
        }
        TestDesignControllerList.newInstance().getTreeController().
                operationGenerateErrorMessage(
                "Profile selected for testplan deleted.",
                "Error");
        return false;
    }

    public boolean isValidProfile() {
        Integer profile = getSelectedProfileId();
        if (profile != null) {
            GlobalDataHandler handler = new GlobalDataHandler();
            if (handler.getProfileById(profile) != null) {
                return true;
            }
        }
        TestDesignControllerList.newInstance().getTreeController().
                operationGenerateErrorMessage(
                "Profile selected for testplan deleted. Please select another profile...",
                "Error");
        return false;
    }

    public Integer getSelectedProfileId() {
        TestDesignTreeNode node = TestDesignControllerList.newInstance().
                getTreeController().getSelectedTreeNode();
        TestDesignTreeNode testPlanNode = findTestPlan(node);
        if (testPlanNode != null) {
            Integer profileId = testPlanNode.getReference().getParameterIntValue(TestDesignTreeParameters.ProfileId.name());
            if (profileId != null) {
                return profileId;
            }
        }
        return null;
    }

    public void useForDataObject() {
        m_isModal = false;
        m_useForCondition = false;
        m_useForDataObject = true;
        if (isValidProfile()) {
            Integer currentProfileId = getSelectedProfileId();
            if (currentProfileId != null) {
                initialize(false);
                setModelProperty(CURRENT_PROFILE_ID,
                        currentProfileId);
                setModelProperty(SHOW_VIEW,
                        true);
            }
        }
    }

    public void useForCondition() {
        m_useForCondition = true;
        m_useForDataObject = false;
        m_isModal = true;
        if (isValidProfile()) {
            Integer currentProfileId = getSelectedProfileId();
            if (currentProfileId != null) {
                initialize(true);
                setModelProperty(CURRENT_PROFILE_ID,
                        currentProfileId);
                setModelProperty(SHOW_VIEW,
                        true);
            }
        }
    }

    public void operationCloseWindow() {
        this.cleanAll();
    }

    public void initialize(boolean modal) {
        AtofProfileKeyTableModel temp = new AtofProfileKeyTableModel();
        temp.addColumn("Key");
        temp.addColumn("Value");
        temp.addColumn("Groupname");
        temp.addColumn("Priority");
        ProfileSettingDetailsModel model = new ProfileSettingDetailsModel(temp);
        ProfileSettingDetailsView view = new ProfileSettingDetailsView(
                TestDesignControllerList.newInstance().
                getParentFrame(),
                modal,
                temp);
        super.addModel(model);
        super.addView(view);
        view.pack();
    }

    public void operationChangeProfileDetailVariable(String key,
            String keyValue) {
        setModelProperty(CURRENT_SELECTED_KEYNAME,
                key);
        setModelProperty(CURRENT_SELECTED_KEYVALUE,
                keyValue);

        if (m_useForDataObject) {
            String currentValue = TestDesignControllerList.newInstance().
                    getDataObjectController().operationGetValue();
            if (currentValue == null) {
                currentValue = "";
            } else if (!currentValue.equals("")) {
                currentValue = currentValue + " ";
            }
            TestDesignControllerList.newInstance().getDataObjectController().
                    operationChangeValue(currentValue + "@GLOBAL_" + key + "@");
        } else if (m_useForCondition) {
            String currentValue = ConditionTextEditorController.createInstance().operationGetConditionText();
            if (currentValue == null) {
                currentValue = "";
            } else if (!currentValue.equals("")) {
                currentValue = currentValue + " ";
            }
            ConditionTextEditorController.createInstance().operationSetConditionText(currentValue + "@GLOBAL_" + key + "@");

        }
    }

    public void operationChangeProfileDetailVariable(String key) {
        setModelProperty(CURRENT_SELECTED_KEYNAME,
                key);
        GlobalDataHandler handler = new GlobalDataHandler();
        int profileId = (Integer) getModelProperty(CURRENT_PROFILE_ID);
        String keyValue = handler.getProfileDetailValue(profileId,
                key);
        setModelProperty(
                CURRENT_SELECTED_KEYVALUE,
                keyValue);

    }

    public String operationGetProfileName(Integer profileId) {
        GlobalDataHandler handler = new GlobalDataHandler();
        T_test_profiles profile = handler.getProfileById(profileId);
        if (profile != null) {
            return profile.getName();
        }
        return "";
    }

    public void operationLoadDetails() {
        int profileId = (Integer) getModelProperty(CURRENT_PROFILE_ID);
        GlobalDataHandler handler = new GlobalDataHandler();
        List<String[]> lst = handler.getProfileKeyDetailsAsArray(profileId);
        AtofProfileKeyTableModel temp = new AtofProfileKeyTableModel();
        temp.addColumn("Key");
        temp.addColumn("Value");
        temp.addColumn("Groupname");
        temp.addColumn("Priority");
        for (int i = 0; i < lst.size(); i++) {
            temp.addRow(lst.get(i));

        }
        setModelProperty(PARAMETER_MODEL_CHANGED,
                temp);
    }

    private TestDesignTreeNode findTestPlan(TestDesignTreeNode node) {
        if (node == null) {
            return null;
        } else if (node.getReference().getType().getName().equals("TestPlan")) {
            return node;
        }

        return findTestPlan(node.getParent());

    }

    public void operationAddPair(String keyName,
            String value) {
        int profileId = (Integer) getModelProperty(CURRENT_PROFILE_ID);
        GlobalDataHandler handler = new GlobalDataHandler();
        boolean result = handler.saveProfileDetail(profileId,
                keyName,
                value);
        if (result) {
            setModelProperty(CURRENT_SELECTED_KEYNAME,
                    keyName);
            setModelProperty(CURRENT_SELECTED_KEYVALUE,
                    value);
            operationLoadDetails();
        } else {
            setModelProperty(SHOW_ERROR,
                    "Unable to add record...");
        }
    }

    public void operationEditPair(String keyName,
            String value) {
        int profileId = (Integer) getModelProperty(CURRENT_PROFILE_ID);
        GlobalDataHandler handler = new GlobalDataHandler();
        boolean result = handler.saveProfileDetail(profileId,
                keyName,
                value);
        if (result) {
            setModelProperty(CURRENT_SELECTED_KEYNAME,
                    keyName);
            setModelProperty(CURRENT_SELECTED_KEYVALUE,
                    value);
            operationLoadDetails();
        } else {
            setModelProperty(SHOW_ERROR,
                    "Unable to edit record...");
        }
    }

    public void operationRemovePair(String keyName) {
        int profileId = (Integer) getModelProperty(CURRENT_PROFILE_ID);
        GlobalDataHandler handler = new GlobalDataHandler();
        boolean result = handler.removeProfileDetail(profileId,
                keyName);
        if (result) {
            setModelProperty(CURRENT_SELECTED_KEYNAME,
                    keyName);
            setModelProperty(CURRENT_SELECTED_KEYVALUE,
                    "");
            operationLoadDetails();
        } else {
            setModelProperty(SHOW_ERROR,
                    "Unable to remove record...");
        }
    }
}
